<!-- 授权配置页面 -->
<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">
            <el-form-item label="提供商名称" prop="providerName">
                <el-input v-model="queryParams.providerName" placeholder="请输入提供商名称" clearable style="width: 240px" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="search" @click="handleQuery">搜索</el-button>
                <el-button icon="refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="Plus" @click="handleAdd"
                    v-hasFunc="'auth:config:add'">新增</el-button>
            </el-col>
            <right-toolbar :showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="主键" align="center" prop="id" :show-overflow-tooltip="true"
                v-if="columns.showColumn('id')">
                <template #default="scope">
                    {{ scope.row.id }}
                </template>
            </el-table-column>
            <el-table-column label="标识码" align="center" prop="provider" :show-overflow-tooltip="true"
                v-if="columns.showColumn('provider')">
                <template #default="scope">
                    {{ scope.row.provider }}
                </template>
            </el-table-column>
            <el-table-column label="提供商名称" align="center" prop="providerName" :show-overflow-tooltip="true"
                v-if="columns.showColumn('providerName')">
                <template #default="scope">
                    {{ scope.row.providerName }}
                </template>
            </el-table-column>
            <el-table-column label="客户端ID" align="center" prop="clientId" :show-overflow-tooltip="true"
                v-if="columns.showColumn('clientId')">
                <template #default="scope">
                    {{ scope.row.clientId }}
                </template>
            </el-table-column>
            <el-table-column label="客户端密钥" align="center" prop="clientSecret" :show-overflow-tooltip="true"
                v-if="columns.showColumn('clientSecret')">
                <template #default="scope">
                    {{ scope.row.clientSecret }}
                </template>
            </el-table-column>
            <el-table-column label="回调地址" align="center" prop="redirectUri" :show-overflow-tooltip="true"
                v-if="columns.showColumn('redirectUri')">
                <template #default="scope">
                    {{ scope.row.redirectUri }}
                </template>
            </el-table-column>
            <el-table-column label="权限范围" align="center" prop="scopes" :show-overflow-tooltip="true"
                v-if="columns.showColumn('scopes')">
                <template #default="scope">
                    {{ scope.row.scopes }}
                </template>
            </el-table-column>
            <el-table-column label="启用状态" align="center" prop="isActive" :show-overflow-tooltip="true"
                v-if="columns.showColumn('isActive')">
                <template #default="scope">
                    <dict-tag :options="sys_normal_disable" :value="scope.row.isActive" />
                </template>
            </el-table-column>
            <el-table-column label="系统内置" align="center" prop="isSystem" :show-overflow-tooltip="true"
                v-if="columns.showColumn('isSystem')">
                <template #default="scope">
                    <dict-tag :options="sys_yes_no" :value="scope.row.isSystem" />
                </template>
            </el-table-column>
            <el-table-column label="创建时间" align="center" prop="createdAt" :show-overflow-tooltip="true"
                v-if="columns.showColumn('createdAt')">
                <template #default="scope">
                    {{ parseTime(scope.row.createdAt) }}
                </template>
            </el-table-column>
            <el-table-column label="创建人" align="center" prop="createdBy" :show-overflow-tooltip="true"
                v-if="columns.showColumn('createdBy')">
                <template #default="scope">
                    {{ scope.row.createdBy }}
                </template>
            </el-table-column>

            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['system:config:edit']">修改</el-button>
                    <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['system:config:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageIndex"
            v-model:limit="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改对话框 -->
        <el-dialog :title="title" v-model="open" width="500px" append-to-body draggable align-center>
            <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
                <el-row :gutter="20">
                    <el-col :lg="24">
                        <el-form-item label="标识码" prop="provider">
                            <el-input v-model="form.provider" placeholder="请输入提供商标识码" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :lg="24">
                        <el-form-item label="提供商名称" prop="providerName">
                            <el-input v-model="form.providerName" placeholder="请输入提供商名称" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :lg="24">
                        <el-form-item label="客户端ID" prop="clientId">
                            <el-input v-model="form.clientId" placeholder="请输入客户端ID" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :lg="24">
                        <el-form-item label="客户端密钥" prop="clientSecret">
                            <el-input v-model="form.clientSecret" placeholder="请输入客户端密钥" clearable show-password />
                        </el-form-item>
                    </el-col>
                    <el-col :lg="24">
                        <el-form-item label="回调地址" prop="redirectUri">
                            <el-input v-model="form.redirectUri" placeholder="请输入回调地址" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :lg="24">
                        <el-form-item label="权限范围" prop="scopes">
                            <el-input v-model="form.scopes" placeholder="请输入权限范围" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :lg="24">
                        <el-form-item label="启用状态" prop="isActive">
                            <el-radio-group v-model="form.isActive">
                                <el-radio v-for="dict in sys_normal_disable" :key="dict.value" :value="dict.value">{{
                                    dict.label
                                    }}</el-radio>
                            </el-radio-group>
                            <!-- <el-select v-model="form." placeholder="请选择启用状态" clearable>
                                <el-option v-for="dict in " :key="dict.value" :label="dict.label"
                                    :value="dict.value" />
                            </el-select> -->
                        </el-form-item>
                    </el-col>
                    <el-col :lg="24">
                        <el-form-item label="系统内置" prop="isSystem" v-if="operateType == 2">
                            <el-select v-model="form.isSystem" placeholder="请选择系统内置" clearable>
                                <el-option v-for="dict in sys_yes_no" :key="dict.value" :label="dict.label"
                                    :value="dict.value" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <el-button type="primary" @click="submitForm" v-if="operateType == 0 || operateType == 1">确
                    定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </template>
        </el-dialog>
    </div>
</template>

<script setup>
import { ref, reactive, onMounted } from "vue";
import { listSysAuthConfig, getSysAuthConfig, delSysAuthConfig, addSysAuthConfig, updateSysAuthConfig } from "@/api/system/authconfig.js";

const { proxy } = getCurrentInstance()

//字典数据
const dicts = ["sys_normal_disable", "sys_yes_no"];
const dictRefs = proxy.useDict(...dicts);
const { sys_normal_disable, sys_yes_no } = dictRefs;

//弹窗操作类型
//0:新增，1：修改，2:查看
const operateType = ref(0)
//表单ref
const formRef = ref();
// 遮罩层
const loading = ref(true);
// 选中数组
const selected = ref([]);
// 显示搜索条件
const showSearch = ref(true);
// 总条数
const total = ref(0);
// 数据集
const dataList = ref([]);
// 弹出层标题
const title = ref("");
// 是否显示弹出层
const open = ref(false);

//默认时间范围
const defaultTime = ref([new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 2, 1, 23, 59, 59)])


// 查询参数
const queryParams = reactive({
    pageIndex: 1,
    pageSize: 10,
    providerName: undefined,
});



const state = reactive({
    single: true,
    multiple: true,
    form: {

    },
    rules: {
        provider: [{ required: true, message: "标识码不能为空", trigger: "blur" }],
        providerName: [{ required: true, message: "供应商名称不能为空", trigger: "blur" }],
        clientId: [{ required: true, message: "客户端ID不能为空", trigger: "blur" }],
        clientSecret: [{ required: true, message: "客户端密钥不能为空", trigger: "blur" }],
    },
    options: {
        // 商品种类 选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
        typeOptions: [],
    }
})

const { form, rules, options, single, multiple } = toRefs(state)


//所有列
const columns = ref([
    { visible: true, prop: 'id', label: '主键' },
    { visible: true, prop: 'provider', label: '标识码' },
    { visible: true, prop: 'providerName', label: '提供商名称' },
    { visible: true, prop: 'clientId', label: '客户端ID' },
    { visible: true, prop: 'clientSecret', label: '客户端密钥' },
    { visible: true, prop: 'redirectUri', label: '回调地址' },
    { visible: true, prop: 'scopes', label: '权限范围' },
    { visible: true, prop: 'isActive', label: '启用状态' },
    { visible: true, prop: 'isSystem', label: '系统内置' },
    { visible: true, prop: 'createdAt', label: '创建时间' },
    { visible: true, prop: 'createdBy', label: '创建人' },
]);

// 获取列表方法
const getList = () => {
    loading.value = true;
    listSysAuthConfig(queryParams).then((response) => {
        dataList.value = response.result;
        total.value = response.totalNum;
        loading.value = false;
    });
};

// 重置表单
const reset = () => {
    form.value = {
        isActive: true,
    }
};

// 搜索按钮操作
const handleQuery = () => {
    queryParams.pageIndex = 1;
    getList();
};

// 重置按钮操作
const resetQuery = () => {
    reset();
    handleQuery();
};

// 多选框选中数据
const handleSelectionChange = (selection) => {
    selected.value = selection.map((item) => item.id);
    single.value = selection.length !== 1;
    multiple.value = !selection.length;
};

// 新增按钮操作
const handleAdd = () => {
    reset();
    operateType.value = 0;
    open.value = true;
    title.value = "添加配置";
};

// 修改按钮操作
const handleUpdate = (row) => {
    reset();
    operateType.value = 1;
    const key = row.id || selected.value;
    getSysAuthConfig(key).then((response) => {
        Object.assign(form.value, response);
        open.value = true;
        title.value = "修改配置";
    });
};


//查看
const handleView = (row) => {
    reset();
    operateType.value = 2;
    var key = row.id;
    getSysAuthConfig(key).then((response) => {
        Object.assign(form.value, response);
        open.value = true;
        title.value = "查看明细";
    });
}

// 提交按钮
const submitForm = () => {
    const validateForm = formRef.value?.validate;
    if (validateForm) {
        validateForm((valid) => {
            if (valid) {
                const apiCall = form.value.id ? updateSysAuthConfig : addSysAuthConfig;
                apiCall(form.value).then(() => {
                    proxy.$modal.msgSuccess("操作成功");
                    open.value = false;
                    getList();
                });
            }
        });
    }
};

//取消弹窗
const cancel = () => {
    open.value = false;
    proxy.resetForm("form")
}

// 删除按钮操作
const handleDelete = (row) => {
    const keys = [].concat(row.id || selected.value);
    proxy.$modal.confirm("是否确认删除编号为" + keys + "的数据项？")
        .then(() => delSysAuthConfig(keys))
        .then(() => {
            getList();
            proxy.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
};

// 导出按钮操作
const handleExport = () => {
    download("system//export", queryParams, `menu_${new Date().getTime()}.xlsx`);
};

// 页面加载时调用获取列表方法
onMounted(getList);
</script>
